package com.cddata;

import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.cddata.entity.Contents;
import com.cddata.util.DBOperation;
import com.cddata.util.HibernateUtil;

@SuppressWarnings("serial")
public class SearchResult extends JPanel {

	public SearchResult() {
		setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
		setVisible(true);
	}

	public void display() {
		try{
			if( list == null ) {
				performStrResult();
			}
			add(DBOperation.getHelperBean().createHeaderPanel("Search contains \'" + list.size() + "\' results...", null));
			add(createResultPanel(list));
			list=null;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void performStrResult() throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Contents.class);
		if( searchStr != null && !"Select".equalsIgnoreCase( typeName ) ) {
			criteria.add(Restrictions.like("contentName","%"+ searchStr+"%"));
			criteria.add(Restrictions.eq("typeId", DBOperation.getTypeId(typeName)));
		} else if( searchStr != null && "Select".equalsIgnoreCase( typeName ) ){
			criteria.add(Restrictions.like("contentName","%"+ searchStr+"%"));
		} else if( searchStr == null && !"Select".equalsIgnoreCase( typeName ) ) {
			criteria.add(Restrictions.eq("typeId", DBOperation.getTypeId(typeName)));
		}
		list = criteria.list();
		session.close();
	}

	@SuppressWarnings("unchecked")
	private JScrollPane createResultPanel( List aList ) {
		JScrollPane scrollPane = null;
		int vsb = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
		int hsb = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
		final String [] columns = {"FileName" , "Type", "Path", "Size", "CD No.." };
		String [][] tableData = new String[aList.size()][5]; 
		int count = 0;
		for( Iterator<Contents> it = aList.iterator(); it.hasNext();) {
			Contents contents = it.next();
			tableData[count][0] = contents.getContentName();
			tableData[count][1] = DBOperation.getHelperBean().getContentTypes( contents.getTypeId().toString() );			
			tableData[count][2] = contents.getContentPath();
			tableData[count][3] = DBOperation.getHelperBean().convertBytesToKBMB(contents.getContentSize());
			tableData[count][4] = contents.getContentHolder();
			count++;
		}
		JTable resultTable = new JTable(tableData,columns);
		resultTable.getColumnModel().getColumn(0).setPreferredWidth(250);
		resultTable.getColumnModel().getColumn(1).setPreferredWidth(30);
		resultTable.getColumnModel().getColumn(2).setPreferredWidth(300);
		resultTable.getColumnModel().getColumn(3).setPreferredWidth(50);
		resultTable.getColumnModel().getColumn(4).setPreferredWidth(40);
		
//		TableModel tm = resultTable.getModel();
//		TableColumn tc = resultTable.getColumnModel().getColumn(2);
//		System.out.println(tc.getIdentifier());
//		TableCellRenderer tcr = resultTable.getDefaultRenderer(tm.getColumnClass(2));
//		Component c = tcr.getTableCellRendererComponent(resultTable,tm.getValueAt(470, 2), false, false, 470, 2); 
		scrollPane = new JScrollPane(resultTable,vsb,hsb);
//		add( scrollPane);
		return scrollPane;
	}
	
	
	@SuppressWarnings("unchecked")
	public List getList() {
		return list;
	}

	@SuppressWarnings("unchecked")
	public void setList(List list) {
		this.list = list;
	}

	public String getSearchStr() {
		return searchStr;
	}

	public void setSearchStr(String searchStr) {
		if( searchStr != null && !searchStr.trim().equalsIgnoreCase("")) 
			this.searchStr = searchStr;
	}
	
	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	
	@SuppressWarnings("unchecked")
	List list = null;
	String typeName = null;
	String searchStr = null;
}
